#! /usr/bin/env python3

import sys
import os
import re
#import shutil

# 检查log文件的时间戳间隔，timediff设置间隔，间隔大于这个值会有输出
timediff = 1.0

def check(syslog):
    timestamp_pattern =  r'\[\s*(\d+\.\d+)\] MVX session:'
    now = -1.0
    old = -1.0

    count = 0
    with open(log) as f:
        for line in f: # 迭代器，对比readlines，大文件内存友好
            count += 1

            matched = re.search(timestamp_pattern, line)
            if matched:
                timestamp = matched.group(1)
                now = float(timestamp)
                if old < 0:
                    old = now

                # check
                diff = now - old
                old = now
                if diff > timediff:
                    print(f'line {count}, diff {diff:.3f}s: {line}', end='')

    print(f'total {count} lines.')


if len(sys.argv) != 2:
    print('Please input one syslog.')
    exit(1)

log = sys.argv[1]
print(f'check: {log}')
check(log)
